package com.danciyixue.boot.dcyx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.danciyixue.boot.core.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.danciyixue.boot.dcyx.mapper.UserExtensionsMapper;
import com.danciyixue.boot.dcyx.service.UserExtensionsService;
import com.danciyixue.boot.dcyx.model.entity.UserExtensions;
import com.danciyixue.boot.dcyx.model.form.UserExtensionsForm;
import com.danciyixue.boot.dcyx.model.query.UserExtensionsQuery;
import com.danciyixue.boot.dcyx.model.vo.UserExtensionsVO;
import com.danciyixue.boot.dcyx.converter.UserExtensionsConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 用户扩展配置信息服务实现类
 *
 * @author youlaitech
 * @since 2025-06-15 16:36
 */
@Service
@DS("dcyx")
@RequiredArgsConstructor
public class UserExtensionsServiceImpl extends ServiceImpl<UserExtensionsMapper, UserExtensions> implements UserExtensionsService {

    private final UserExtensionsConverter userExtensionsConverter;

    /**
    * 获取用户扩展配置信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<UserExtensionsVO>} 用户扩展配置信息分页列表
    */
    @Override
    public IPage<UserExtensionsVO> getUserExtensionsPage(UserExtensionsQuery queryParams) {
        Page<UserExtensionsVO> pageVO = this.baseMapper.getUserExtensionsPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取用户扩展配置信息表单数据
     *
     * @param id 用户扩展配置信息ID
     * @return 用户扩展配置信息表单数据
     */
    @Override
    public UserExtensionsForm getUserExtensionsFormData(Long id) {
        UserExtensions entity = this.getById(id);
        return userExtensionsConverter.toForm(entity);
    }
    
    /**
     * 新增用户扩展配置信息
     *
     * @param formData 用户扩展配置信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveUserExtensions(UserExtensionsForm formData) {
        UserExtensions entity = userExtensionsConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新用户扩展配置信息
     *
     * @param id   用户扩展配置信息ID
     * @param formData 用户扩展配置信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateUserExtensions(Long id,UserExtensionsForm formData) {
        UserExtensions entity = userExtensionsConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除用户扩展配置信息
     *
     * @param ids 用户扩展配置信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteUserExtensionss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的用户扩展配置信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public UserExtensionsVO getUserExtensionsByUserToken() {

        Long UserID = SecurityUtils.getUserId();
        UserExtensions entity = this.getOne(new LambdaQueryWrapper<UserExtensions>()
                .eq(UserExtensions::getUserId, UserID)
        );
        return userExtensionsConverter.toVO(entity);

    }

    @Override
    public boolean saveOrUpdateUserExtensions(UserExtensionsForm formData) {

        Long UserID = SecurityUtils.getUserId();
        UserExtensions entity = this.getOne(new LambdaQueryWrapper<UserExtensions>()
                .eq(UserExtensions::getUserId, UserID)
        );

        if (entity == null) {
            entity = userExtensionsConverter.toEntity(formData);
            entity.setUserId(UserID);
            return this.save(entity);
        } else {
            formData.setId(entity.getId());
            entity = userExtensionsConverter.toEntity(formData);
            entity.setId(entity.getId());
            return this.updateById(entity);
        }
    }
}
